<?php

namespace Modules\Admin\Http\Controllers;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Modules\Admin\Models\Config as ConfigModel;
class LoginController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
		$admin = Auth::guard("admin")->user();
		if($admin){
			return redirect()->guest(route('admin.index.index'))->send();exit();
		}
		$config = Cache::get('config');
		if(!$config){
			$config = ConfigModel::with('image_one')->find(1)->toarray();
			Cache::put('config',$config,3600);
		}
        return view('admin::login/index',compact('config'));
    }

    /**
     * 登陆数据处理
     */
    public function login(Request $request)
    {
        $admin = request(['username', 'password']);
        if (true == \Auth::guard('admin')->attempt($admin)) {
			return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => '登录成功！',
				'url'=>route('admin.index.index')
			],201);
        }else{
			return response()->json([
				'status'=>0,
				'error' => '用户名或密码错误！',
				'msg' => '',
				'url'=>route('admin.index.index')
			],201);
		}
    }
}
